Imports PTT.CarRes.Data

Public Class ClassQueryInvCarColor
    Public Function Add(ByVal InvCarColor As QueryInvCarColor, ByVal Strconn As String) As Boolean
        Dim ClsInvCarColor As New ClassInvCarColor
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarColor.ConnectionOpen(Strconn)
        Dim ret As Boolean = False
        With ClsInvCarColor
            .ColorId = InvCarColor.ColorId
            .ColorName = InvCarColor.ColorName
            .ColorStatus = InvCarColor.ColorStatus
            ret = .USP_Save(Conn)
        End With
        ClsInvCarColor.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Edit(ByVal InvCarColor As QueryInvCarColor, ByVal Strconn As String) As Boolean
        Dim ClsInvCarColor As New ClassInvCarColor
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarColor.ConnectionOpen(Strconn)
        Dim ret As Boolean = False
        With ClsInvCarColor
            .ColorId = InvCarColor.ColorId
            .Load(Conn)
            .ColorName = InvCarColor.ColorName
            .ColorStatus = InvCarColor.ColorStatus
            ret = .USP_Update(Conn)
        End With
        ClsInvCarColor.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Delete(ByVal InvCarColor As QueryInvCarColor, ByVal Strconn As String) As Boolean
        Dim ClsInvCarColor As New ClassInvCarColor
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarColor.ConnectionOpen(Strconn)
        Dim ret As Boolean = False
        With ClsInvCarColor
            .ColorId = InvCarColor.ColorId
            .Load(Conn)
            ret = .USP_Delete(Conn)
        End With
        ClsInvCarColor.ConnectionClose(Conn)
        Return ret
    End Function

    Public Function Search(Optional ByVal ParaColorId As String = "", _
    Optional ByVal ParaColorName As String = "", _
    Optional ByVal ParaColorStatus As String = "", Optional ByVal Strconn As String = "") As QueryInvCarColors
        Dim ClsInvCarColor As New ClassInvCarColor
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarColor.ConnectionOpen(Strconn)
        Dim ret As QueryInvCarColors = Nothing
        Dim str As String = ""
        Dim Condition As String = ""
        str &= " Select * from InvCarColor"
        If ParaColorId & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(ColorId = " & ParaColorId & ")"
        End If
        If ParaColorName & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(ColorName like '%" & ParaColorName & "%')"
        End If
        If ParaColorStatus & "" <> "" Then
            If Condition & "" <> "" Then
                Condition &= " and "
            End If
            Condition &= "(ColorStatus like '%" & ParaColorStatus & "%')"
        End If

        If Condition & "" <> "" Then
            str &= " where "
            str &= Condition
        End If
        Dim ds As DataSet
        With ClsInvCarColor
            ds = .BindData(Conn, , , str)
            If Not ds Is Nothing Then
                Dim i As Integer
                Dim results As New QueryInvCarColors

                Try
                    For i = 0 To ds.Tables(0).Rows.Count - 1
                        With ds.Tables(0).Rows(i)

                            results.Add(New QueryInvCarColor( _
                              .Item("ColorId") & "", _
                              .Item("ColorName") & "", _
                              .Item("ColorStatus") & "" _
                            ))
                        End With
                    Next
                    ret = results
                Catch ex As Exception
                    Return Nothing
                    'Throw New DALException(ex.Message)
                End Try
            End If
        End With
        ClsInvCarColor.ConnectionClose(Conn)
        Return ret
    End Function
    Public Function Load(Optional ByVal ParaColorId As String = "", Optional ByVal Strconn As String = "") As QueryInvCarColors
        Dim ClsInvCarColor As New ClassInvCarColor
        Dim Conn As OleDb.OleDbConnection
        Conn = ClsInvCarColor.ConnectionOpen(Strconn)
        Dim ret As QueryInvCarColors = Nothing
        Dim str As String = ""
        Dim Condition As String = ""
        str &= " Select * from InvCarColor"
        If ParaColorId & "" <> "" Then
            If Condition & "" <> "" Then Condition &= " and "
            Condition &= "(ColorId = " & ParaColorId & ")"
        End If

        If Condition & "" <> "" Then
            str &= " where "
            str &= Condition
        Else
            Return Nothing
            Exit Function
        End If
        Dim ds As DataSet
        With ClsInvCarColor
            ds = .BindData(Conn, , , str)
            If Not ds Is Nothing Then
                Dim i As Integer
                Dim results As New QueryInvCarColors

                Try
                    For i = 0 To ds.Tables(0).Rows.Count - 1
                        With ds.Tables(0).Rows(i)

                            results.Add(New QueryInvCarColor( _
                              .Item("ColorId") & "", _
                              .Item("ColorName") & "", _
                              .Item("ColorStatus") & "" _
                            ))
                        End With
                    Next
                    ret = results
                Catch ex As Exception
                    Return Nothing
                    'Throw New DALException(ex.Message)
                End Try
            End If
        End With
        ClsInvCarColor.ConnectionClose(Conn)
        Return ret
    End Function
End Class